package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.bas.entity.BasDepartment;
import com.kingint.mbg.bas.mapper.BasDepartmentMapper;
import com.kingint.mbg.dto.MemberReturn;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.mapper.UmsMemberMapper;
import com.kingint.web.admin.base.service.BasDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
@Slf4j
public class BasDepartmentServiceImpl extends ServiceImpl<BasDepartmentMapper, BasDepartment> implements BasDepartmentService {

    @Resource
    private BasDepartmentMapper basDepartmentMapper;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private UmsMemberMapper memberMapper;

    @Override
    public int insertBaseDepartmentInfo(BasDepartment basDepartment) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();//对应的酒店id
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店");
            return 0;
        }
        basDepartment.setHotelId(hotelId);
        basDepartment.setCreateTime(new Date());
        int insert = basDepartmentMapper.insert(basDepartment);
        return insert;
    }

    @Override
    public int deleteDepartmentInfo(Integer id) {
        BasDepartment department = new BasDepartment();
        department.setId(id);
        department.setStatus(1);//删除
        int i = basDepartmentMapper.updateById(department);
        return i;
    }

    @Override
    public IPage<BasDepartment> getDepartmentList(BasDepartment basDepartment, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if(StringUtils.isEmpty(hotelId)){
            log.debug("当前账号没有关联对应的酒店");
            return null;
        }


        IPage<BasDepartment> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BasDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0)
                .eq("hotel_id",hotelId);
        String name = basDepartment.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        IPage<BasDepartment> info = basDepartmentMapper.selectPage(page, wrapper);
        return info;
    }

    @Override
    public int updateDepartment(BasDepartment basDepartment) {
        int i = basDepartmentMapper.updateById(basDepartment);
        return i;
    }

    @Override
    public List<BasDepartment> getDepartmentPerson() {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0)
                .eq("hotel_id",hotelId);
        List<BasDepartment> list = basDepartmentMapper.selectList(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            for (BasDepartment info:list) {
                List<MemberReturn> departmentUser = memberMapper.getDepartmentUser(info.getId(), 1);//查询上班的
                info.setMemberReturns(departmentUser);
            }
        }
        return list;
    }

    @Override
    public List<BasDepartment> getAllDepartmentList() {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        QueryWrapper<BasDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0)
                .eq("hotel_id",hotelId);
        List<BasDepartment> list = basDepartmentMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<MemberReturn> getPersonList(Integer departmentId, int i) {
        List<MemberReturn> departmentUser = memberMapper.getDepartmentUser(departmentId, i);
        return departmentUser;
    }
}
